package cn.buk.tms.dao;

import cn.buk.tms.entity.base.Airline;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Component;

import java.util.List;
import jakarta.transaction.Transactional;

/**
 * Created with IntelliJ IDEA.
 * User: yfdai
 * Date: 14-10-15
 * Time: 下午3:59
 * To change this template use File | Settings | File Templates.
 * @author yfdai
 */
@Component
@Transactional
public class AirlineDaoImpl extends AbstractDao implements AirlineDao {

    private static final Logger logger = LogManager.getLogger(AirlineDaoImpl.class);

    @Override
    public int create(Airline airline) {
        List<Airline> airlineList = em.createQuery("select o from Airline o where o.twoCode = :twoCode", Airline.class)
                .setParameter("twoCode", airline.getTwoCode())
                .getResultList();
        if (airlineList.size() > 0) {
          return -1;
        }

        em.persist(airline);

        return 1;
    }

    @Override
    public Airline getAirlineByTwoCode(String twoCode) {
        List<Airline> airlineList = em.createQuery("select o from Airline o where o.twoCode = :twoCode", Airline.class)
                .setParameter("twoCode", twoCode)
                .getResultList();
        if (airlineList.size() > 0) {
          return airlineList.get(0);
        } else {
          return null;
        }
    }

    @Override
    public List<Airline> getAllAirlines() {
        return em.createQuery("select o from Airline o order by o.twoCode", Airline.class)
                .getResultList();
    }
}
